package com.example.designmodel;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;

public class TemplateActivity extends AppCompatActivity {
    public static void launch(Context context) {
        Intent intent = new Intent(context, TemplateActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_template);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });

        template();
    }

    private void template(){
        Model1 model1 = new Model1();
        model1.setAlarm(false);
        model1.run();
        Model2 model2 = new Model2();
        model2.run();
    }


    public static abstract class TemplateModel{
        protected abstract void start();
        protected abstract void stop();
        protected abstract void sound();
        protected abstract boolean isAlarm();
        final public void run(){
            start();
            if(isAlarm()){
                sound();
            }
            stop();
        }

    }

    public static class Model1 extends TemplateModel{
        private boolean isAlarm;
        @Override
       protected void start() {
            Log.i("test","model1 start");
        }

        @Override
        protected void stop() {
            Log.i("test","model1 stop");
        }

        @Override
        protected void sound() {
            Log.i("test","model1 sound");
        }

        public void setAlarm(boolean isAlarm){
            this.isAlarm = isAlarm;
        }

        @Override
        protected boolean isAlarm() {
            return isAlarm;
        }
    }

    public static class Model2 extends TemplateModel{
        @Override
        protected void start() {
            Log.i("test","model2 start");
        }

        @Override
        protected void stop() {
            Log.i("test","model2 stop");
        }

        @Override
        protected void sound() {
            Log.i("test","model2 sound");
        }

        @Override
        protected boolean isAlarm() {
            return false;
        }
    }

}
